package org.microservice.controller;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.microservice.entity.User;
import org.microservice.service.feign.UserFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

//@Import(FeignClientsConfiguration.class)
@RestController
public class MovieController {

//    private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class);

//    @Autowired
//    private RestTemplate restTemplate;
    //    @Autowired
//    private DiscoveryClient discoveryClient;
//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
    @Resource
    private UserFeignClient userFeignClient;

    @Resource
    private RestTemplate restTemplate;
//    private UserFeignClient adminFeignClient;


//    @Autowired
//    public MovieController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
//        userFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
//                .logLevel(feign.Logger.Level.FULL)
//                .requestInterceptor(new BasicAuthRequestInterceptor("user", "psd111"))
//                .target(UserFeignClient.class, "http://microservice-provider-user/");
//        adminFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
//                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "psd222"))
//                .target(UserFeignClient.class, "http://microservice-provider-user/");
//    }

    @GetMapping("/user/{id}")
    public User findByIdUser(@PathVariable("id") Long id) {
        return userFeignClient.findById(id);
    }

//    @GetMapping("/admin/{id}")
//    public User findByIdAdmin(@PathVariable Long id) {
//        return adminFeignClient.findById(id);
//    }

//    @GetMapping("/user-instance/{id}")
//    public List<ServiceInstance> showInfo() {
//        System.out.printf("=== if equals: %s ===", discoveryClient == this.discoveryClient);
//        return discoveryClient.getInstances("microservice-provider-user");
//    }
//
//    @GetMapping("/log-instance")
//    public void logUserInstance() {
//        ServiceInstance serviceInstance = loadBalancerClient.choose("microservice-provider-user");
//        MovieController.LOGGER.info("instance info: {}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
//    }


//    @HystrixCommand(fallbackMethod = "findByIdDefault", commandProperties = {@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")})
//    @GetMapping("/user/{id}")
//    public User findById(@PathVariable Long id) {
//        return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
//    }
//
//    public User findByIdDefault(Long id) {
//        User user = new User();
//        user.setId(-1L);
//        user.setName("默认用户");
//        return user;
//    }

    // 测试sidecar访问
    @GetMapping("/sidecar/index")
    public String viaHome(){
        return this.restTemplate.getForObject("http://microservice-sidecar/index", String.class);
    }
}
